package com.wolfstreet.wsonline;

import java.io.File;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.widget.TabHost;
import android.widget.Toast;

import com.wolfstreet.wsonline.util.DBHelper;
import com.wolfstreet.wsonline.util.FileHelper;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;

/**
 * @author sy
 * 
 */
public class WSOApplication extends Application {

	/**
	 * APP对象
	 */
	private static WSOApplication instance;

	/**
	 * 客户端是否是首次启动
	 */
	public boolean isFirstStartUp = true;

	/**
	 * 是否已经初始化
	 */
	private boolean hasInit;

	/**
	 * 全局资源对象
	 */
	public Resources res;

	/**
	 * 屏宽
	 */
	public int screenWidth;

	/**
	 * 屏高
	 */
	public int screenHeight;

	/**
	 * APP 页面高
	 */
	public int contentHeight;

	/**
	 * 
	 */
	public float density;

	/**
	 * 
	 */
	public String deviceId;

	/**
	 * SD卡路径
	 */
	public String SD_PATH;

	/**
	 * sd卡是否存在
	 */
	public boolean sdCardIsExist;

	/**
	 * 公共提示对象
	 */
	public Toast myToast;

	/**
	 * tab导航条
	 */
	public TabHost MenuTab;

	/**
	 * 数据库操作类
	 */
	public DBHelper db;

	/**
	 * 版本号
	 */
	public String engine_version = "1.0";

	/**
	 * 登录状态
	 */
	public String login_key = "";

	public ImageLoader imageLoader;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		instance = this;
		myToast = Toast.makeText(this, "", Toast.LENGTH_SHORT);

	}

	public static WSOApplication getInstance() {
		return instance;
	}

	/**
	 * 设置页面高度
	 */
	public void setContentHeight(int statusHeight) {
		contentHeight = screenHeight - statusHeight;
	}

	/**
	 * 初始化
	 * 
	 * @param activity
	 */
	public void init(Activity activity, Handler handler) {
		// if (hasInit)
		// return;
		hasInit = true;
		res = getResources();
		if (db == null) {
			db = new DBHelper(this);
		}

		initSystemParams(activity);

		isFirstStartUp = !FileHelper.isFileExist(new File(
				Constant.RES_FIRST_DATA));
		initFileSystem();

		if (handler != null) {
			handler.sendEmptyMessageDelayed(0, 500);
		}
	}

	/**
	 * 系统参数初始化
	 * 
	 * @param activity
	 */
	public void initSystemParams(Activity activity) {
		DisplayMetrics dm = new DisplayMetrics();
		activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
		screenWidth = dm.widthPixels;
		screenHeight = dm.heightPixels;
		density = dm.density;

		TelephonyManager tm = (TelephonyManager) activity
				.getSystemService(Context.TELEPHONY_SERVICE);
		deviceId = tm.getDeviceId();

		try {
			String packName = activity.getPackageName();
			PackageInfo info = activity.getPackageManager().getPackageInfo(
					packName, 0);
			engine_version = info.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 文件系统初始化
	 */
	public void initFileSystem() {
		sdCardIsExist = FileHelper.isExistSD();
		FileHelper.makeDir();
	}

	public ImageLoader getImageHelper() {
		if (imageLoader == null) {
			imageLoader = ImageLoader.getInstance();
			ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
					getApplicationContext())
					.memoryCacheExtraOptions(480, 800)
					// max width, max height
					.threadPoolSize(3)
					// 线程池内加载的数量
					.threadPriority(Thread.NORM_PRIORITY - 2)
					// 降低线程的优先级保证主UI线程不受太大影响
					.denyCacheImageMultipleSizesInMemory()
					.memoryCache(new LruMemoryCache(5 * 1024 * 1024))
					// 建议内存设在5-10M,可以有比较好的表现
					.memoryCacheSize(5 * 1024 * 1024)
					.discCacheSize(50 * 1024 * 1024)
					.discCacheFileNameGenerator(new Md5FileNameGenerator())
					.tasksProcessingOrder(QueueProcessingType.LIFO)
					.discCacheFileCount(100)
					// 缓存的文件数量
					// .discCache(new UnlimitedDiscCache(cacheDir))
					.defaultDisplayImageOptions(
							DisplayImageOptions.createSimple())
					.imageDownloader(
							new BaseImageDownloader(getApplicationContext(),
									5 * 1000, 30 * 1000)) // connectTimeout (5
															// s),
															// readTimeout (30
															// s)
					.writeDebugLogs() // Remove for release app
					.build();
			ImageLoader.getInstance().init(config);
		}
		return imageLoader;
	}

}
